<template>
  <div class="container">
    <div class="input-container">
      <div class="login-title">欢迎登录</div>
      <el-form :model="loginform" @submit.native.prevent="login">
        <el-form-item label="用户名" prop="username" required>
          <el-input v-model="loginform.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" required>
          <el-input type="password" v-model="loginform.password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginform: {
        username: '',
        password: '',
      },
      code: null
    }
  },
  mounted() {
    this.getCode()
  },
  methods: {
    getCode() {
      this.$http.get('/users/backend/code').then(res => {
        if (res.data.code === 1) {
          this.code = res.data.data.data
        }
      }).catch(err => {
        console.error(err);
        this.$message.error('获取验证码失败');
      });
    },
    login() {
      this.$http.post('/users/backend/login', this.loginform).then((res) => {
        if (res.data.code === 1) {
          this.$message.success('登录成功');
          sessionStorage.setItem('user', JSON.stringify(res.data.user));
          sessionStorage.setItem('token', res.data.token);
          this.$router.push('/user');
        } else {
          this.$message.error(res.data.message);
        }
      }).catch(err => {
        console.error(err);
        this.$message.error('登录失败');
      });
    },
  },
}
</script>

<style scoped>
/* 保持原有背景图片不变 */
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url("../../pic/1.jpg") no-repeat center center / cover;
}

/* 优化输入框和按钮的视觉效果 */
.input-container {
  width: 100%;
  max-width: 400px;
  padding: 30px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #2c3e50;
  font-size: 1.5em;
}

.el-form-item__label {
  color: #7f8c8d;
  font-weight: 500;
}

.el-input__inner {
  border: 1px solid #c0c4cc;
  border-radius: 4px;
  padding: 12px;
  font-size: 14px;
}

.el-button {
  background-color: #3498db;
  color: white;
  font-size: 16px;
  padding: 10px 20px;
  border-radius: 20px;
  transition: background-color 0.3s;
}

.el-button:hover {
  background-color: #2980b9;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .container {
    height: auto;
    padding: 20px;
  }

  .login-title {
    font-size: 1.2em;
  }

  .el-input__inner {
    padding: 10px;
  }

  .el-button {
    padding: 8px 15px;
  }
}
</style>